package com.lewis.module.m2.listener;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lewis.api.system.entity.BotQq;
import com.lewis.module.m2.api.bot.service.IBotQqService;
import com.lewis.module.m2.utils.BotUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 程序启动后运行该类
 *
 * @author Lewis
 */
@Slf4j
@Component
public class CustomApplicationRunner implements ApplicationRunner {

	@Resource
	private IBotQqService botQqService;

	@Override
	public void run(ApplicationArguments args) {
		// 程序开启时判断是否存在需要登录的机器人
		List<BotQq> botQqList = botQqService.list(
				new LambdaQueryWrapper<BotQq>()
						.eq(BotQq::getAutoLogin, 1)
		);
		if (botQqList.size() > 0) {
			// 若存在则首先启动机器人框架
			BotUtils.botStart();
			// 登录机器人
			log.info("对所有自动登录的机器人做登录操作......");
			for (BotQq botQq : botQqList) {
				BotUtils.init(Long.parseLong(botQq.getQq()), botQq.getPassword());
				botQq.setOnlineState(1);
				botQqService.updateById(botQq);
			}
			log.info("所有机器人成功登录。");
		}
	}
}